package de.lulusoft.anothertorrent.core.rtorrent;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class File {
	
	public static final String PROP_CHANGED = "filePropChanged";
	
	private Long completed_chunks;
	private String frozen_path;
	private Long created;
	private Long open;
	private Long last_touched;
	private Long match_depth_next;
	private Long match_depth_prev;
	private Long offset;
	private String path;
	private String path_components;
	private Long path_depth;
	private Long priority;
	private Long rage_first;
	private Long range_second;
	private Long size_bytes;
	private Long size_chunks;
	private Long index;
	private Torrent torrent = null;
		
	private PropertyChangeSupport propertyChangeSupport = null;
	
	public File(Long completed_chunks, String frozen_path, Long created,
			Long open, Long last_touched, Long match_depth_next,
			Long match_depth_prev, Long offset, String path,
			String path_components, Long path_depth, Long priority,
			Long rage_first, Long range_second, Long size_bytes,
			Long size_chunks, Long index, Torrent torrent) {
		this.completed_chunks = completed_chunks;
		this.frozen_path = frozen_path;
		this.created = created;
		this.open = open;
		this.last_touched = last_touched;
		this.match_depth_next = match_depth_next;
		this.match_depth_prev = match_depth_prev;
		this.offset = offset;
		this.path = path;
		this.path_components = path_components;
		this.path_depth = path_depth;
		this.priority = priority;
		this.rage_first = rage_first;
		this.range_second = range_second;
		this.size_bytes = size_bytes;
		this.size_chunks = size_chunks;
		this.index = index;
		this.torrent = torrent;
		
		propertyChangeSupport = new PropertyChangeSupport(this);
	}
	
	/**
	 * Updates this file with the new values.
	 * Dont notify propertychangelisteners, so update can be called from non gui thread.
	 * Returns 0 if no change occurred, 1 if a change occurred.
	 * @return
	 */
	public int update(Long completed_chunks, String frozen_path, Long created,
			Long open, Long last_touched, Long match_depth_next,
			Long match_depth_prev, Long offset, String path,
			String path_components, Long path_depth, Long priority,
			Long rage_first, Long range_second, Long size_bytes,
			Long size_chunks) {
		
		boolean changed = false;
		
		if (!this.completed_chunks.equals(completed_chunks)) { this.completed_chunks = completed_chunks; changed = true; }
		if (!this.frozen_path.equals(frozen_path)) { this.frozen_path = frozen_path; changed = true; }
		if (!this.created.equals(created)) { this.created = created; changed = true; }
		if (!this.open.equals(open)) { this.open = open; changed = true; }
		if (!this.last_touched.equals(last_touched)) { this.last_touched = last_touched; changed = true; }
		if (!this.match_depth_next.equals(match_depth_next)) { this.match_depth_next = match_depth_next; changed = true; }
		if (!this.match_depth_prev.equals(match_depth_prev)) { this.match_depth_prev = match_depth_prev; changed = true; }
		if (!this.offset.equals(offset)) { this.offset = offset; changed = true; }
		if (!this.path.equals(path)) { this.path = path; changed = true; }
		if (!this.path_components.equals(path_components)) { this.path_components = path_components; changed = true; }
		if (!this.path_depth.equals(path_depth)) { this.path_depth = path_depth; changed = true; }
		if (!this.priority.equals(priority)) { this.priority = priority; changed = true; }
		if (!this.rage_first.equals(rage_first)) { this.rage_first = rage_first; changed = true; }
		if (!this.range_second.equals(range_second)) { this.range_second = range_second; changed = true; }
		if (!this.size_bytes.equals(size_bytes)) { this.size_bytes = size_bytes; changed = true; }
		if (!this.size_chunks.equals(size_chunks)) { this.size_chunks = size_chunks; changed = true; }
		
		if (changed)
		{	
			return 1;
		}
		else
			return 0;
		
	}

	public Long getCompleted_chunks() {
		return completed_chunks;
	}

	public void setCompleted_chunks(Long completed_chunks) {
		this.completed_chunks = completed_chunks;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.completed_chunks);
	}

	public String getFrozen_path() {
		return frozen_path;
	}

	public void setFrozen_path(String frozen_path) {
		this.frozen_path = frozen_path;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.frozen_path);
	}

	public Long getCreated() {
		return created;
	}

	public void setCreated(Long created) {
		this.created = created;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.created);
	}

	public Long getOpen() {
		return open;
	}

	public void setOpen(Long open) {
		this.open = open;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.open);
	}

	public Long getLast_touched() {
		return last_touched;
	}

	public void setLast_touched(Long last_touched) {
		this.last_touched = last_touched;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.last_touched);
	}

	public Long getMatch_depth_next() {
		return match_depth_next;
	}

	public void setMatch_depth_next(Long match_depth_next) {
		this.match_depth_next = match_depth_next;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.match_depth_next);
	}

	public Long getMatch_depth_prev() {
		return match_depth_prev;
	}

	public void setMatch_depth_prev(Long match_depth_prev) {
		this.match_depth_prev = match_depth_prev;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.match_depth_prev);
	}

	public Long getOffset() {
		return offset;
	}

	public void setOffset(Long offset) {
		this.offset = offset;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.offset);
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.path);
	}

	public String getPath_components() {
		return path_components;
	}

	public void setPath_components(String path_components) {
		this.path_components = path_components;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.path_components);
	}

	public Long getPath_depth() {
		return path_depth;
	}

	public void setPath_depth(Long path_depth) {
		this.path_depth = path_depth;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.path_depth);
	}

	public Long getPriority() {
		return priority;
	}

	public void setPriority(Long priority) {
		this.priority = priority;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.priority);
	}

	public Long getRage_first() {
		return rage_first;
	}

	public void setRage_first(Long rage_first) {
		this.rage_first = rage_first;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.rage_first);
	}

	public Long getRange_second() {
		return range_second;
	}

	public void setRange_second(Long range_second) {
		this.range_second = range_second;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.range_second);
	}

	public Long getSize_bytes() {
		return size_bytes;
	}

	public void setSize_bytes(Long size_bytes) {
		this.size_bytes = size_bytes;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.size_bytes);
	}

	public Long getSize_chunks() {
		return size_chunks;
	}

	public void setSize_chunks(Long size_chunks) {
		this.size_chunks = size_chunks;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.size_chunks);
	}
	public Integer getDonePercent()
	{
		Double d = (double)this.completed_chunks / (double)this.size_chunks * (double)100;
		return ((Double)Math.floor(d)).intValue();
	}
	
	public Long getIndex() {
		return index;
	}

	public void setIndex(Long index) {
		this.index = index;
	}

	public Torrent getTorrent() {
		return torrent;
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener)
	{
		propertyChangeSupport.addPropertyChangeListener(PROP_CHANGED, listener);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener listener)
	{
		propertyChangeSupport.removePropertyChangeListener(PROP_CHANGED, listener);
	}
}
